package com.heima.media.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.ChannelDto;
import com.heima.media.entity.WmChannel;
import com.heima.media.mapper.WmChannelMapper;
import com.heima.media.service.IChannelService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class ChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements IChannelService {

    @Override
    public PageResponseResult<List<WmChannel>> listChannelPage(ChannelDto dto) {
        String name = dto.getName();
        Page<WmChannel> channelPage = new Page<>(dto.getPage(), dto.getSize());
        // Wrapper<WmChannel> query = new QueryWrapper<>();
        LambdaQueryWrapper<WmChannel> query = new LambdaQueryWrapper<>();
        Page<WmChannel> iPage;
        if (!StringUtils.isEmpty(name)) {
            // query = new QueryWrapper<WmChannel>().like("name", name);
            query = query.like(WmChannel::getName, name);
        }
        // 如果有状态
        if (!StringUtils.isEmpty(dto.getStatus())) {
            query.eq(WmChannel::getStatus, dto.getStatus());
        }
        iPage = page(channelPage, query);
        PageResponseResult<List<WmChannel>> result = new PageResponseResult<>(
                dto.getPage(), dto.getSize(), iPage.getTotal(), iPage.getRecords()
        );
        result.setCode(AppHttpCodeEnum.SUCCESS.getCode());
        return result;
    }

    @Override
    public ResponseResult insertChannel(WmChannel channel) {
        // 须要对channel进行验证
        if (channel == null || StringUtils.isEmpty(channel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 然后还需要判断频道是否已经存在
        LambdaQueryWrapper<WmChannel> query = new LambdaQueryWrapper<>();
        query.eq(WmChannel::getName, channel.getName());
        WmChannel wmChannel = this.getOne(query);
        if (wmChannel != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);// 已经存在,返回错误提示
        }
        // 都没有问题,补全数据并保存
        channel.setCreatedTime(new Date());
        this.save(channel);
        return ResponseResult.okResult(channel.getId());
    }

    @Override
    public ResponseResult deleteChannel(Long id) {
        // 先根据id找到channel

        // 如果channel在news中有关联数据,那么是不能删除的,这里要用到微服务远程调用news了
        // 暂时没有,直接删除
        this.removeById(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateChannel(WmChannel channel) {
        // 判断修改后的频道名是否与现有频道名冲突
        String name = channel.getName();
        LambdaQueryWrapper<WmChannel> query = new LambdaQueryWrapper<>();
        query.eq(WmChannel::getName, name);
        WmChannel one = this.getOne(query);
        // 通过请求的频道名查询到了频道信息,且这个频道的Id不是请求的这个id,证明冲突
        if (one != null && !one.getId().equals(channel.getId())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        // 没什么需要补全的信息,直接更新就好了
        this.updateById(channel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getChannels() {
        LambdaQueryWrapper<WmChannel> query = new LambdaQueryWrapper<>();
        query.eq(WmChannel::getStatus,1);
        List<WmChannel> list = this.list(query);
        return ResponseResult.okResult(list);
    }


}
